package com.cictec.middleware.tde.rm.protocol.rm.decode;

import java.nio.ByteBuffer;

import org.apache.mina.core.session.IoSession;

import com.cictec.middleware.commons.utils.BinaryUtils;
import com.cictec.middleware.tde.exception.MessageDecoderException;
import com.cictec.middleware.tde.rm.protocol.AbstractMessageDecoder;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage.Header;
import com.cictec.middleware.tde.rm.protocol.annotion.TerminalMessageDecoder;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x21TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.utils.RmBinaryUtils;

/**
 * 
 * @author lhl
 *
 */
@TerminalMessageDecoder(0x21)
public class Rm0x21Decoder extends AbstractMessageDecoder {

	@Override
	public TerminalMessage decode(Header header, ByteBuffer in, IoSession session) throws MessageDecoderException {

		Rm0x21TerminalMessage tw = new Rm0x21TerminalMessage(header);
		tw.setCreateTime(RmBinaryUtils.dwordToDateRM(in));
		tw.setLat(RmBinaryUtils.dwordToLatLngRM(in));
		tw.setLng(RmBinaryUtils.dwordToLatLngRM(in));
		tw.setSpeed(BinaryUtils.unsignedByteToInt(in));
		tw.setAngle(BinaryUtils.unsignedByteToInt(in));
		tw.setAltitude(BinaryUtils.wordToInt(in));
		tw.setStationNum(BinaryUtils.wordToInt(in));
		tw.setWarnType(BinaryUtils.unsignedByteToInt(in));
		if (tw.getWarnType() == 0x07 || tw.getWarnType() == 0x08) {
			tw.setStandValue(in.get());
		} else {
			tw.setStandValue(BinaryUtils.unsignedByteToInt(in));
		}
		tw.setStart(in.get());
		if (tw.getWarnType() == 0x07 || tw.getWarnType() == 0x08) {
			tw.setMaxValue(in.get());
		} else {
			tw.setMaxValue(BinaryUtils.unsignedByteToInt(in));
		}
		tw.setWarnDuration(BinaryUtils.wordToInt(in));
		tw.setReversed(BinaryUtils.wordToInt(in));

		return tw;
	}

}
